<template>
  <div ref="containerRef" class="common-full-page">
    <div ref="searchRef" class="common-search-view">
      <t-form ref="formRef" :data="formData" :colon="true" layout="inline" :label-width="0">
        <form-item v-for="(item, index) in list" :key="index" :form-data="formData" :data="item"></form-item>
        <div class="opt">
          <div class="item">
            <t-button class="common-button" theme="primary" @click="search">搜索</t-button>
            <t-button class="common-button" variant="outline" @click="reset">重置</t-button>
          </div>
          <div class="item">
            <t-button class="common-button" theme="primary" @click="create">新增</t-button>
          </div>
        </div>
      </t-form>
    </div>
    <div class="common-table-wrap shadow" :style="{ height: `calc(100% - ${searchHeight}px)` }">
      <t-base-table class="common-table" row-key="index" :data="tableData" max-height="100%" :columns="columns">
        <template #opt="{ row }">
          <div class="common-options">
            <div class="item" @click="update(row)">
              <svg-icon class="icon" name="options-edit " />
              <t-link theme="primary" class="link" :underline="true">编辑</t-link>
            </div>
            <div class="item" @click="del(row)">
              <svg-icon class="icon" name="options-edit" />
              <t-link theme="primary" class="link" :underline="true">删除</t-link>
            </div>
            <div class="item" :class="{ disabled }" @click="disabled ? null : detail(row)">
              <svg-icon class="icon" name="options-detail" />
              <t-link theme="primary" class="link" :underline="true">详情</t-link>
            </div>
            <t-dropdown :options="dropdownOptions" :min-column-width="120" trigger="click" @click="handleClickDropdown">
              <div class="item">
                <svg-icon class="icon" name="options-more" />
                <t-link theme="primary" class="link" :underline="true">更多</t-link>
                <svg-icon class="icon" name="options-down_arrow" />
              </div>
            </t-dropdown>
          </div>
        </template>
      </t-base-table>
      <pagination-view
        v-if="!!pageData.total"
        :current="pageData.params.page"
        :total="pageData.total"
        :page-size="pageData.params.size"
        @page-change="handlePageChange"
      ></pagination-view>
    </div>

    <edit-dialog ref="editDialogRef" :title="editDialogTitle" :data="detailData" @sure="getData"></edit-dialog>
    <detail-dialog ref="detailDialogRef" title="详情" :data="detailData" @sure="getData"></detail-dialog>
    <message-dialog
      ref="messageDialogRef"
      title="连接测试失败"
      tip="测试设备20022362"
      type="tip"
      :show-cancel="true"
      @sure="messageDialogSure"
      @close="messageDialogClose"
    ></message-dialog>
  </div>
</template>

<script setup lang="ts">
import { tryit } from 'radash';
import { MessagePlugin } from 'tdesign-vue-next';
import { computed, onMounted, ref } from 'vue';

import mockApi from '@/api/mock';
import { ListParams, ResultItem } from '@/api/model/mockModel';
import { SUCCESS_CODE } from '@/constants';
import { useListPage } from '@/hooks/useListPage';
import { useLoading } from '@/hooks/useLoading';
import { useTableHeight } from '@/hooks/useTableHeight';
import { requestCancel } from '@/utils/request';

import DetailDialog from './comopnents/DetailDialog.vue';
import EditDialog from './comopnents/EditDialog.vue';
import { columns, INIT_DATA, list } from './datas';

const containerRef = ref<HTMLDivElement>();
const editDialogRef = ref<InstanceType<typeof EditDialog>>();
const detailDialogRef = ref<InstanceType<typeof DetailDialog>>();
const messageDialogRef = ref();

const formData = ref({ ...INIT_DATA });
const tableData = ref([]);
const detailData = ref<ResultItem>();
const dropdownOptions = ref([
  { content: '菜单一', value: '1' },
  { content: '菜单二', value: '2' },
  { content: '菜单三', value: '3' },
]);

const disabled = ref(false);

const { pageData, handlePageChange, search, reset, formRef } = useListPage(getData);
const { searchRef, searchHeight } = useTableHeight();
const { loadingStart, loadingEnd } = useLoading(containerRef);

const editDialogTitle = computed(() => {
  return detailData.value?.id ? '编辑弹框' : '新增弹框';
});

const handleClickDropdown = (data: any) => {
  const { content } = data;
  console.log('content', data, content);
};

const create = () => {
  detailData.value = null;
  editDialogRef.value.open();
};

const update = (data: ResultItem) => {
  detailData.value = data;
  editDialogRef.value.open();
};

const detail = (data: ResultItem) => {
  detailData.value = data;
  detailDialogRef.value.open();
};

const del = (data: ResultItem) => {
  detailData.value = data;
  messageDialogRef.value.open();
};

const messageDialogSure = async () => {
  const [err, res] = await tryit(mockApi.del)({ id: detailData.value.id });

  if (!err && res.code === SUCCESS_CODE) {
    messageDialogRef.value.close();
    await MessagePlugin.success('操作成功');
    await getData();
  }
};

const messageDialogClose = () => {
  requestCancel('/mock/del', 'post');
};

async function getData() {
  loadingStart();

  const postData: ListParams = { ...pageData.params, search: { ...formData.value } };
  const [err, res] = await tryit(mockApi.list)(postData);

  if (!err && res.code === SUCCESS_CODE) {
    const { list, total } = res.data;
    tableData.value = list;
    pageData.total = total;
  }
  loadingEnd();
}

onMounted(() => {
  getData();
});
</script>

<style lang="less" scoped></style>
